package com.lexis.wallpaper;

import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.service.wallpaper.WallpaperService;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;

public class SimpleWallpaper extends WallpaperService {

    //private final Handler mHandler = new Handler();

    private class SimpleEngine extends Engine {
		private static final String TAG = "SimpleWallpaper.Engine";
		private static final String TAG_MOVE = "SimpleWallpaperMove";

		private final Paint mPaint = new Paint();
		private final SimpleWallpaper mWallpaper;
        private boolean mVisible;
		private Bitmap mBitmap;
		private int mXOffset;
		private int mXWallSize;
		private int mXCanvasSize;

        //private final Runnable mDrawCube = new Runnable() {
        //    public void run() {
        //        drawWall();
        //    }
        //};
        //--------------------------------------------------------------------------------------------------------------

        SimpleEngine(SimpleWallpaper wallpaper) {
        	mWallpaper = wallpaper;

            //mStartTime = SystemClock.elapsedRealtime();
        }
        //--------------------------------------------------------------------------------------------------------------

        @Override
        public void onCreate(SurfaceHolder surfaceHolder) {
        	Log.d(TAG, "onCreate");
            super.onCreate(surfaceHolder);
            
            // By default we don't get touch events, so enable them.
            //setTouchEventsEnabled(true);
        }
        //--------------------------------------------------------------------------------------------------------------

        @Override
        public void onDestroy() {
        	Log.d(TAG, "onDestroy");
            super.onDestroy();
            //mHandler.removeCallbacks(mDrawCube);
        }
        //--------------------------------------------------------------------------------------------------------------

        @Override
        public void onVisibilityChanged(boolean visible) {
        	Log.d(TAG, "onVisibilityChanged: " + visible);
            mVisible = visible;
            drawWall();
        }
        //--------------------------------------------------------------------------------------------------------------

        @Override
        public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            Log.d(TAG, "onSurfaceChanged - canvas: " + width + "x" + height);
            super.onSurfaceChanged(holder, format, width, height);
            
            mXCanvasSize = width;

            //WallpaperManager wMgr = WallpaperManager.getInstance(mWallpaper);
            
            int wallWidth = getDesiredMinimumWidth();
            int wallHeight = getDesiredMinimumHeight();
            Log.d(TAG, "onSurfaceChanged - wallpaper: " + wallWidth + "x" + wallHeight);
            mXWallSize = wallWidth;
            
           	mBitmap = BitmapManager.getBitmap(getResources(), wallWidth, /*wallHeight*/height, !isPreview());
        }
        //--------------------------------------------------------------------------------------------------------------

        @Override
        public void onSurfaceCreated(SurfaceHolder holder) {
        	Log.d(TAG, "onSurfaceCreated");
            super.onSurfaceCreated(holder);
        }
        //--------------------------------------------------------------------------------------------------------------

        @Override
        public void onSurfaceDestroyed(SurfaceHolder holder) {
        	Log.d(TAG, "onSurfaceDestroyed");
            super.onSurfaceDestroyed(holder);
            mVisible = false;
            mBitmap = null;
        }
        //--------------------------------------------------------------------------------------------------------------

        @Override
        public void onOffsetsChanged(float xOffset, float yOffset,
                float xStep, float yStep, int xPixels, int yPixels) {
        	Log.d(TAG_MOVE, "onOffsetChanged: offset (" + xOffset + "," + yOffset + "), " +
        							      "step (" + xStep + "," + yStep + "), " + xPixels + ", " + yPixels);
            mXOffset = (int)((mXWallSize - mXCanvasSize) * xOffset);
            drawWall();
        }
        //--------------------------------------------------------------------------------------------------------------

        @Override
        public void onTouchEvent(MotionEvent event) {
//            if (event.getAction() == MotionEvent.ACTION_MOVE) {
//                mTouchX = event.getX();
//                mTouchY = event.getY();
//            } else {
//                mTouchX = -1;
//                mTouchY = -1;
//            }
            super.onTouchEvent(event);
        }
        //--------------------------------------------------------------------------------------------------------------

        void drawWall() {
        	if (mVisible) {
	            final SurfaceHolder holder = getSurfaceHolder();
	
	            Canvas c = null;
	            try {
	                c = holder.lockCanvas();
	                if (c != null) {	                	
	                	c.drawBitmap(mBitmap, -mXOffset, 0, mPaint);
	                }
	            } finally {
	                if (c != null) holder.unlockCanvasAndPost(c);
	            }
        	}

            // Reschedule the next redraw
//            mHandler.removeCallbacks(mDrawCube);
//            if (mVisible) {
//                mHandler.postDelayed(mDrawCube, 1000 / 25);
//            }
        }
	}
    //------------------------------------------------------------------------------------------------------------------
    //------------------------------------------------------------------------------------------------------------------
	
	@Override
	public Engine onCreateEngine() {
		return new SimpleEngine(this);
	}
    //------------------------------------------------------------------------------------------------------------------

}
